#define NOMINMAX
#include "Entity2D.h"
#include <d3dx9math.h>
#include <iostream>
using namespace pg1;
	Entity2D::Entity2D()
		:
	m_fPosX(0.0f),
	m_fPosY(0.0f),
	m_fRotation(0.0f),
	m_fScaleX(1.0f),
	m_fScaleY(1.0f),
	m_pkTransformationMatrix(new D3DXMATRIX())
	{}

	Entity2D::CollisionResult Entity2D::checkCollision(Entity2D& rkEntity2D) const{
		float fOverlapX = std::max( 0.0f, 
									std::min( m_fPosX + fabs( scaleX() ) / 2.0f,rkEntity2D.posX() + fabs( rkEntity2D.scaleX() ) / 2.0f) -  
									std::max( m_fPosX - fabs( scaleX() ) / 2.0f,rkEntity2D.posX() - fabs( rkEntity2D.scaleX() ) / 2.0f)
		);
		float fOverlapY = std::max( 0.0f,
									std::min( m_fPosY + fabs( scaleY() ) / 2.0f,  rkEntity2D.posY() + fabs( rkEntity2D.scaleY() ) / 2.0f) -  
									std::max( m_fPosY - fabs( scaleY() ) / 2.0f, rkEntity2D.posY() - fabs( rkEntity2D.scaleY() ) / 2.0f)
		);

		if(fOverlapX != 0.0f && fOverlapY != 0.0f){
			if(fOverlapX > fOverlapY){
				return CollisionVertical;
			}else{
				return CollisionHorizontal;
			}
		}
		return NoCollision;
	}

	Entity2D::~Entity2D(){}

	void Entity2D::setPos(float fPosX,float fPosY)
	{
		m_fPrevPosX=m_fPosX;
		m_fPrevPosY=m_fPosY;

		m_fPosX=fPosX;
		m_fPosY=fPosY;
		updateLocalTransformation();
	}
	void Entity2D::setRotation(float fRotation)
	{
		m_fRotation=fRotation;
		updateLocalTransformation();
	}
	void Entity2D::setScale(float fScaleX,float fScaleY)
	{
		m_fScaleX=fScaleX;
		m_fScaleY=fScaleY;
		updateLocalTransformation();
	}
	void Entity2D::returnToPos(float fPosX, float fPosY){
		m_fPosX=fPosX;
		m_fPosY=fPosY;

		updateLocalTransformation();
	}
	void Entity2D::updateLocalTransformation()
	{
		//translacion matrix
		D3DXMATRIX kTransMat;
		D3DXMatrixTranslation(&kTransMat,m_fPosX,m_fPosY,0);

		//rotacion en el eje z de nuestra matrix
		D3DXMATRIX kRotationMat;
		D3DXMatrixRotationZ(&kRotationMat,m_fRotation);

		D3DXMATRIX kScaleMat;
		D3DXMatrixScaling(&kScaleMat,m_fScaleX,m_fScaleY,1);

		//build matrix

		D3DXMatrixIdentity(m_pkTransformationMatrix);
		D3DXMatrixMultiply(m_pkTransformationMatrix,&kTransMat,m_pkTransformationMatrix);
		D3DXMatrixMultiply(m_pkTransformationMatrix,&kRotationMat,m_pkTransformationMatrix);
		D3DXMatrixMultiply(m_pkTransformationMatrix,&kScaleMat,m_pkTransformationMatrix);
	}
	const Matrix Entity2D::transformationMatrix(){
		return(m_pkTransformationMatrix);
	}
	float Entity2D::posX(){
		return m_fPosX;
	}
	float Entity2D::posY(){
		return m_fPosY;
	}
	float Entity2D::scaleX() const{
		return m_fScaleX;
	}
	float Entity2D::scaleY() const{
		return m_fScaleY;
	}
	float Entity2D::prevPosX() const{
		return m_fPrevPosX;
	}
	float Entity2D::prevPosY() const{
		return m_fPrevPosY;
	}
	void Entity2D::drawAABB(Renderer& rkRenderer) const{
	 static ColorVertex s_akAABBVertices[5];
	 static bool s_bIsInitialized = false;
	 if(!s_bIsInitialized){
	  s_bIsInitialized = true;

	  s_akAABBVertices[0].x = -0.5; s_akAABBVertices[0].y = -0.5; s_akAABBVertices[0].z = 0.0; s_akAABBVertices[0].color = PG1_COLOR_RGB(255,50,50);
	  s_akAABBVertices[1].x = -0.5; s_akAABBVertices[1].y = 0.5; s_akAABBVertices[1].z = 0.0; s_akAABBVertices[1].color = PG1_COLOR_RGB(255,70,70);
	  s_akAABBVertices[2].x = 0.5; s_akAABBVertices[2].y = 0.5; s_akAABBVertices[2].z = 0.0; s_akAABBVertices[2].color = PG1_COLOR_RGB(255,30,30);
	  s_akAABBVertices[3].x = 0.5; s_akAABBVertices[3].y = -0.5; s_akAABBVertices[3].z = 0.0; s_akAABBVertices[3].color = PG1_COLOR_RGB(255,15,15);
	  s_akAABBVertices[4].x = -0.5; s_akAABBVertices[4].y = -0.5; s_akAABBVertices[4].z = 0.0; s_akAABBVertices[4].color = PG1_COLOR_RGB(255,95,90);
	 }

	 rkRenderer.setCurrentTexture(NoTexture);
	 rkRenderer.setMatrix(World, m_pkTransformationMatrix );
	 rkRenderer.Draw(s_akAABBVertices, pg1::LineStrip, 5);
	}